﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shove.Web.UI;

public partial class UserControls_MyOrder : UCBase 
{
    public UserControls_MyOrder()
    {
        Name = "我的订单";

        swpas = new ShoveWebPartAttribute[12];

        swpas[0] = new ShoveWebPartAttribute_Int("控件宽度", "1,1004", 759, "控件宽度");
        swpas[1] = new ShoveWebPartAttribute_Int("导航单元格高度", "1,500", 23, "导航单元格高度");
        swpas[2] = new ShoveWebPartAttribute_Int("导航单元格宽度", "1,600", 139, "导航单元格宽度");
        swpas[3] = new ShoveWebPartAttribute_Int("表格数据行高", "1,200", 23, "表格数据行高");
        swpas[4] = new ShoveWebPartAttribute_Int("每页显示的行数", "1,40", 8, "每页显示的行数");

        swpas[5] = new ShoveWebPartAttribute_Color("控件边框颜色", "#E7C890", "控件边框颜色");
        swpas[6] = new ShoveWebPartAttribute_Text("文本CSS样式", "", "blank12", "文本CSS样式");

        swpas[7] = new ShoveWebPartAttribute_Image("导航背景图片1", "", "images/UserControls/bg_gold_23.jpg", "导航背景图片1");
        swpas[8] = new ShoveWebPartAttribute_Image("导航背景图片2", "", "images/UserControls/bg_gold_23_2.jpg", "导航背景图片2");

        swpas[9] = new ShoveWebPartAttribute_Text("导航文本样式", "", "black12", "导航文本样式");
        swpas[10] = new ShoveWebPartAttribute_Text("表头样式", "", "blank12", "表头样式");
        swpas[11] = new ShoveWebPartAttribute_Int("表头行高", "1,200", 23, "表头行高");
    }

    private void Initlayout()
    {
        //tabctr.Width = swpAttributes[0];
        //tabctr.BgColor = swpAttributes[5];

        //tdsign.Width = swpAttributes[2];
        //tdsign.Height = swpAttributes[1];
        //tdsign.Style.Add("background-image", "url('" + swpAttributes[7] + "')");
        //tdsign.Attributes.Add("class", swpAttributes[9]);

        //tdspace.Style.Add("background-image", "url('" + swpAttributes[8] + "')");
        gvOrder.RowStyle.CssClass = swpAttributes[6];
        gvOrder.RowStyle.Height = new Unit(swpAttributes[3]);
        gvOrder.HeaderStyle.Height = new Unit(swpAttributes[11]);
        gvOrder.HeaderStyle.CssClass = swpAttributes[10];
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Initlayout();

        if (!IsPostBack)
        {
            if (_Customer == null)
            {
                Response.Redirect("Default.aspx?PN=Login");
            }
            else
            {
                BindData();
            }
        }
    }

    private void BindData()
    {
        long ShopID = _Site.ID;
        this.Page1.PageSize = Shove._Convert.StrToInt(swpAttributes[4], 8);
        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        Page1.GridID = gvOrder;
        Page1.Condition = " where CustomerID=" + _Customer.ID + " and ShopID=" + ShopID ;
        Page1.TableName = "V_Shop_SalesMaster";
    }

    protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //设置表格样式
        e.Row.Attributes.Add("class", "black12");
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Style.Add("background-image", "url(images/UserControls/bg_hui_23_2.jpg)");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.BackColor = System.Drawing.Color.FromName("#FFFFFF");

            LinkButton btnSelect = (LinkButton)e.Row.Cells[5].Controls[0];
            LinkButton btnDel = (LinkButton)e.Row.Cells[6].Controls[0];

            int Status = Shove._Convert.StrToInt(e.Row.Cells[7].Text, 0);

            if (Status == 0)//0 未付款 1 已付款 2 已出库  3 已取消
            {
                 //e.Row.Cells[3].Text = "付款";
                 btnSelect.Enabled = true;
                 btnDel.Enabled = true;
            }
            else
            {
                 //e.Row.Cells[3].Text = "已付款";
                 btnSelect.Enabled = false;
                 btnDel.Enabled = false;
            }

            if (btnSelect.Enabled == true || btnDel.Enabled == true)
            {
                btnSelect.Attributes.Add("onclick", "return confirm('确定要付款吗？')");
                btnDel.Attributes.Add("onclick", "return confirm('确定取消订单吗？')");
            }
        }
    }

    /// <summary>
    /// 取消订单
    /// </summary>
    protected void gvOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DAL.Tables.tbl_Shop_SalesMaster SalesMaster = new DAL.Tables.tbl_Shop_SalesMaster();        
        int ID = Shove._Convert.StrToInt(gvOrder.DataKeys[e.RowIndex].Value.ToString(), 0);
        SalesMaster.Status.Value = 3;  //0 未付款 1 已付款 2 已出库  3 已取消

        SalesMaster.Update("ID=" + ID.ToString());
        Response.Redirect("Default.aspx?PN=member_homeOrder");
    }

    /// <summary>
    ///  付款
    /// </summary>
    protected void gvOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        long SalesID = Shove._Convert.StrToLong(gvOrder.DataKeys[e.NewSelectedIndex].Value.ToString(), 0);

        double Money = 0;

        try
        {
            Shove._Convert.StrToDouble(gvOrder.Rows[e.NewSelectedIndex].Cells[4].Text.ToString(), 0);
        }
        catch { }

        this.Response.Write("<script language='javascript'>window.open('OnlinePay/Alipay02/Send.aspx?PayMoney=" + Money.ToString() + "&RelatedOrderType=1&RelatedOrderID=" + SalesID.ToString() + "')</script>");
    }

}
